"use strict"
var call = require("../../utils/request.js")
const app = getApp()
Page({
  data: {
    userInfo: '',
    birthday: '请选择日期',
    // cardid: '请输入身份证号',
    // phone: '请输入手机号'
    cardid: '',
    phone: '',
    money:'',
    bili:''
  },

  onLoad: function(e) {
    if (wx.getStorageSync("uid")) {

      var call = require("../../utils/request.js")
      var url = 'getchongzhi';
      call.getData(url, this.getchongzhi, this.fail)

    } else {

      setTimeout(() => {
        wx.showToast({
          title: '请先授权登录',
          icon: "none",
        });
        setTimeout(() => {
          wx.switchTab({
            url: '../mine/mine'
          })
        }, 2000)
      }, 0);
    }

  },


  mobileInput: function (e) {
    var money = e.detail.value
    console.log(money)
    this.setData({
      money: money
    })
},



getchongzhi: function getchongzhi(data) {
  this.setData({
    bili: data.data
  })

},
  pay: function pay() {
      var that = this
      wx.request({
        url: "https://www.yunzhuke.net/api/data/chongzhi",
        data: {
          openid: wx.getStorageSync("openid"),
          chongzhijine: that.data.money,
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          console.log(res.data)
          if (res.data == 234) {
            wx.showToast({
              title: '充值失败,请重新支付',
              icon: 'none',
              duration: 3000
            })
          }else {

            let order_no = res.data.data.order_no    //订单号
            let timeStamp = String(res.data.data.timeStamp)  //服务器端返回的时间戳，必须强转字符串，微信要求的
            let nonceStr = res.data.data.nonce_str //服务器返回的随机字符串
            // let package = 'prepay_id=' + res.data.prepay_id //package需要这么填写，自己注意
            let packageres = 'prepay_id=' + res.data.data.prepay_id
            let paySign = res.data.data.paySign //支付签名

            // 拉起微信支付
            wx.requestPayment({
              'timeStamp': timeStamp,
              'nonceStr': nonceStr,
              'package': packageres,
              'signType': 'MD5',
              'paySign': paySign,
              success: function (res) {
                //接口调用成功的回调函数
                console.log('支付success')
                console.log(res.errMsg)
                if (res.errMsg == 'requestPayment:ok'){
                  setTimeout(() => {
                    wx.showToast({
                      title: '支付成功',
                      icon: "none",
                    });
                    setTimeout(() => {
                      wx.reLaunch({
                        url: '../pageck/money'
                      })
                    }, 2000)
                  }, 0);
                }
              },
              fail: function (res) {
                wx.showToast({
                  title: '充值失败',
                  icon: 'none',
                  duration: 3000
                })
                //接口调用失败的回调函数
              },
              complete: function (res) {
            
                console.log(res)
                //接口调用结束的回调函数（调用成功、失败都会执行）
              }
            })
          }

        }
      })

  },

  gohome: function (data) {
    wx.reLaunch({
      url: '../index/index',
    })
  },

  chongzhi: function (data) {
    wx.reLaunch({
      url: '../pageck/recharge',
    })
  },





})